<template>
	<view class="other-login m-f-ac">
		<block v-for="(item, index) in providerList" :key="index">
			<view class="m-f-jcc m-f1" @tap="tologin(item)">
				<view class="icon iconfont m-f-jcc" :class="['icon-' + item.icon]"></view>
			</view>
		</block>
	</view>
</template>

<script>
	export default {
		props: {
			noback: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				providerList: []
			}
		},
		mounted() {
			this.getLoginAuth()
		},
		methods: {
			getLoginAuth() { // 获取当前登录渠道
				uni.getProvider({
					service: 'oauth',
					success: (result) => {
						this.providerList = result.provider.map((value) => {
							let providerName = '';
							let icon = '';
							switch (value) {
								case 'weixin':
									providerName = '微信登录'
									icon = 'weixin'
									break;
								case 'qq':
									providerName = 'QQ登录'
									icon = 'qq'
									break;
								case 'sinaweibo':
									providerName = '新浪微博登录'
									icon = 'weibo'
									break;
								case 'alipay':
									providerName = '支付宝登录'
									icon = ''
									break;
								case 'baidu':
							}
							return {
								name: providerName,
								icon: icon,
								id: value
							}
						});
						
					},
					fail: (error) => {
						console.log('获取登录通道失败', error);
					}
				});
			},
			tologin(provider) {
				uni.login({
					provider: provider.id,
					// #ifdef MP-ALIPAY
					scopes: 'auth_user', //支付宝小程序需设置授权类型
					// #endif
					success: (res) => {
						uni.getUserInfo({
							provider: provider.id,
							success: (infoRes) => {
								this.loginEvent(this.User.__formatOtherLogin(provider.id, Object.assign(infoRes, res)))
							}
						})
						console.log('login success:', res);
						// 更新保存在 store 中的登录状态
						console.log('登录成功，保存到本地存储，修改当前用户登录状态')
						// #ifdef MP-WEIXIN
						console.warn('如需获取openid请参考uni-id: https://uniapp.dcloud.net.cn/uniCloud/uni-id')
						uni.request({
							url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/user-center',
							method:'POST',
							data: {
								action: 'loginByWeixin',
								params: {
									code: res.code,
									platform: 'mp-weixin'
								}
							},
							success(res) {
								console.log(res);
								if (res.data.code !== 0) {
									console.log('获取openid失败：', res.result.msg);
									return
								}
								uni.setStorageSync('openid', res.data.openid)
							},
							fail(err) {
								console.log('获取openid失败：', err);
							}
						})
						// #endif
					},
					fail: (err) => {
						console.log('login fail:', err);
					}
				});
			},
			async loginEvent(data) {
				let res = await this.User.login({
					url: '/user/otherlogin',
					data: data,
					Noback: this.noback
				})
				if (res) {
					// 登录成功，重新加载数据
					this.$emit('logining')
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.other-login {
		padding: 20rpx 80rpx;
		.icon {
			width: 100rpx;
			height: 100rpx;
			border:1rpx solid;
			border-radius: 100%;
			font-size: 55rpx;
			color: #FFFFFF;
		}
		.icon-weixin {
			background: #2CAEFC;
		}
		.icon-weibo {
			background: #2BD19B;
		}
		.icon-qq {
			background: #FC7729;
		}
	}
</style>
